//
//  ProjectViewController.m
//  BigToBig
//
//  Created by liuyi on 7/14/16.
//  Copyright © 2016 cn.cloudtp. All rights reserved.
//

#import "ProjectViewController.h"
#import "HomeFunctionView.h"
#import "HomeProjectTableViewCell.h"
#import "ProjectDetailViewController.h"
#import "ProjectModel.h"

@interface ProjectViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic , retain)HomeFunctionView *homeFunctionView;
@property (nonatomic , retain)UITableView *mainTableView;
@property (nonatomic , strong)NSMutableArray *projectModelArr;
@property (nonatomic , assign)NSInteger limit;
@property (nonatomic , assign)NSInteger modelCount;

@end

@implementation ProjectViewController

- (void)viewWillAppear:(BOOL)animated{
//    [self createData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.limit = 10;
    [self createData];
// 创建功能区(排序 and 筛选)
    self.homeFunctionView = [[HomeFunctionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    [self.view addSubview:self.homeFunctionView];
    
    
// 创建mainTableView
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, kScreenWidth, kScreenHeight - 64 - 84 - 49)];
    [self.view addSubview:self.mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[HomeProjectTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HomeProjectTableViewCell class])];
    self.mainTableView.separatorStyle = NO;
    self.mainTableView.bounces = NO;
    
}


#pragma mark - tableView 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeProjectTableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeProjectTableViewCell class]) forIndexPath:indexPath];
    ProjectModel *model = self.projectModelArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.bigtobig.com/%@",model.projectLogo]] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    cell.titleLabel.text = model.projectName;
    cell.describeLabel.text = model.productIntroduction;
    if (model.road_show_state.integerValue == 1) {
        cell.timeLabel.text = [NSString stringWithFormat:@"路演时间:%@",model.showStartTime];
        cell.statusLabel.text = @"已结束";
    }
    else if(model.road_show_state.integerValue == 0){
        cell.timeLabel.text = [NSString stringWithFormat:@"项目发布时间:%@",model.createProjectTime];
        cell.statusLabel.text = @"未开始";
    }
    else if (model.road_show_state.integerValue == 0){
        cell.timeLabel.text = [NSString stringWithFormat:@"路演时间:%@",model.showStartTime];
        cell.statusLabel.text = @"已取消";
    }
    
    NSArray *markArr = [NSMutableArray array];
    markArr = [model.areas componentsSeparatedByString:@"|"];
    cell.markArray = markArr;
    cell.checkLabel.text = [NSString stringWithFormat:@"  已查看 : %@",model.sawCount];
    cell.focusLabel.text = [NSString stringWithFormat:@"  已关注 : %@",model.noticeCount];
    return cell;
}

// cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ProjectDetailViewController *projectDetailVC  = [[ProjectDetailViewController alloc]init];
    ProjectModel *model = self.projectModelArr[indexPath.row];
    projectDetailVC.project_id = model.project_id;
    if (indexPath.row == 1) {
        projectDetailVC.isRoadShowCreated = YES;
    }
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:projectDetailVC animated:YES];
    
}

#pragma mark - 获取数据
- (void)createData{
    
    self.projectModelArr = [NSMutableArray array];
    NSString *urlStr = [NSString stringWithFormat:@"http://61.135.176.43:6080/bigtobig/basic/project/get_projects?project_audit_state=1&limit=%ld&offset=0",self.limit];
    NSDictionary *params = @{@"limit":[NSString stringWithFormat:@"%ld",self.limit]};
    [HttpTool textHTMLPostWithURL:urlStr params:params success:^(id json) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *projectListArr = [dict objectForKey:@"projectlist"];
        for (NSDictionary *userDic in projectListArr) {
                    ProjectModel *model = [ProjectModel ProjectModelWithDict:userDic];
                    [self.projectModelArr addObject:model];
            
        }
        NSString *countStr = [dict objectForKey:@"count"];
        if (self.modelCount + self.limit < countStr.integerValue) {
            self.modelCount = self.modelCount + self.limit;
        }
        else{
            self.modelCount = countStr.integerValue;
        }
        [self.mainTableView reloadData];
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
